package com.sec.etech.common.util;

import com.sec.etech.sys.util.PropertyUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version : 1.0
 * @auther : 齐马的作品
 * @date : 2020/5/28 13:06
 * @description:
 */
public class BasicUtil {

    /**
     * 判断是否为空
     * @param obj
     * @return
     */
    public static Boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj.equals("") || obj.equals("null")) {
            return true;
        }

        if (obj instanceof String) {
            if (((String) obj).trim().length() == 0) {
                return true;
            }
        } else if (obj instanceof Collection) {
            if (((Collection) obj).size() == 0) {
                return true;
            }
        } else if (obj instanceof Map) {
            if (((Map) obj).size() == 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    /**
     * 文本连接，能自动去掉空字符串和null值
     * @param txtList 需连接的文本
     * @param symbol 分隔字符
     * @return String
     * @author PengMaochang
     * @date 2021/9/1 10:10
     */
    public static String concat(List<String> txtList, String symbol) {
        if (isEmpty(txtList)) {
            return "";
        }
        StringBuilder concatString = new StringBuilder();
        for (int i = 0; i < txtList.size(); i++) {
            if (!isEmpty(txtList.get(i))) {
                concatString.append(txtList.get(i));
                if (i != txtList.size() - 1 && !isEmpty(symbol)) {
                    concatString.append(symbol);
                }
            }
        }
        return concatString.toString();
    }

    public static boolean isProd() {
        return PropertyUtil.getPropertyService().getByAlias("systemEnvironmentVariables").equals("prod");
    }

}
